/* 
 * Dynamic allocating array data structure.
 * Copyright (C) 2006 Bob Zimmermann <rpz@cse.wustl.edu>
 *
 *  This file is a part of BAT.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * darray.h
 * 
 * Simple dynamic array data structure.
 *
 * $Id: darray.h 1409 2006-09-20 20:30:33Z rpz $
 *
 */

#ifndef DARRAY_H
#  define DARRAY_H

#include "config.h"
#  include<stdlib.h>

typedef void *(*darray_copy_op) (const void *);
typedef int (*darray_order_op) (const void **, const void **);
typedef int (*darray_identify_op) (const void *, const void *);
typedef void (*darray_delete_op) (void *, int);

typedef unsigned da_pos_t;

typedef struct _darray_ops_t
{
  darray_copy_op copy;
  darray_order_op order;
  darray_identify_op identify;
  darray_delete_op delete;
} darray_ops_t;

darray_ops_t *darray_ops_new (darray_copy_op, 
			      darray_order_op , darray_identify_op, darray_delete_op);
darray_ops_t *darray_ops_copy (const darray_ops_t * dao);
void darray_ops_delete (darray_ops_t * dao, int befrei);
typedef struct _darray_t
{
  da_pos_t n_elts;
  da_pos_t pos;
  void **data;
  darray_ops_t *ops;
} darray_t;

extern darray_ops_t darray_ops_ops;

darray_t *darray_new (da_pos_t init_size, darray_ops_t * dao);
darray_t *darray_copy (const darray_t * da);
void darray_delete (darray_t * da, int);

void *darray_get (darray_t * da, da_pos_t index);
const void *darray_peek (const darray_t * da, da_pos_t index);
void *darray_find (darray_t * da, const void *key, darray_identify_op ident);

void darray_set (darray_t * da, da_pos_t index, const void *data);
void darray_insert (darray_t * da, da_pos_t index, const void *data);
da_pos_t darray_place (darray_t * da, const void *data, darray_order_op order);
void darray_add (darray_t * da, const void *data);
void darray_clear(darray_t* da, int);

void *darray_remove (darray_t * da, da_pos_t pos, int befrei);
void darray_pop (darray_t * da);

da_pos_t darray_pos (darray_t * da, const void *ptr);
__EXTERN_INLINE__ da_pos_t darray_size (darray_t * da);
void darray_gc (darray_t * da);

void darray_sort (darray_t * da, darray_order_op order);
void **darray_get_array (darray_t * da);
void **darray_release(darray_t *da, da_pos_t *sz, int);

#endif
